﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
namespace KMS.Core
{ 
    [AttributeUsage(AttributeTargets.Class)]
    public class ConnAttribute : Attribute
    {
        public ConnAttribute(string connName)
        {
            Name = connName;
        }
 
        public string Name { get; set; }
    }
    //[AttributeUsage(AttributeTargets.Class)]
    //public class TableAttribute : Attribute
    //{
    //    public TableAttribute(string tblName)
    //    {
    //        Name = tblName;
    //    } 
    //    public string Name { get; set; }
    //}

    public static class AttributeHelper
    {
        private static ConcurrentDictionary<Type, string> connNameMap = new ConcurrentDictionary<Type, string>();
        /// <summary>
        /// 取得数据库链接名称
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetConnName(this Type type)
        {
            string name;

            if (!connNameMap.TryGetValue(type, out name))
            {
                var attribute = type.GetCustomAttributes(typeof(ConnAttribute), false).FirstOrDefault() as ConnAttribute;
                if (attribute != null)
                {
                    name = attribute.Name;
                }
                if (string.IsNullOrEmpty(name))
                {
                    name = Kernel.DEFAULT_CONN;
                } 

                connNameMap[type] = name;
            }
            return name;
        }
        [Serializable]
        public class IdentityAttribute : Attribute
        {
        }

        [Serializable]
        public class IgnoreAttribute : Attribute
        {
        }

        [Serializable]
        public class KeyAttribute : Attribute
        {
        }

        //static ConcurrentDictionary<RuntimeTypeHandle, string> TypeTableNameMap = new ConcurrentDictionary<RuntimeTypeHandle, string>();
        //public static string GetTableName(this Type type)
        //{
             
        //    string name;
        //    if (!TypeTableNameMap.TryGetValue(type.TypeHandle, out name))
        //    {
        //        name = type.Name;
        //        if (type.IsInterface && name.StartsWith("I"))
        //            name = name.Substring(1);

        //        //NOTE: This as dynamic trick should be able to handle both our own Table-attribute as well as the one in EntityFramework 
        //        var tableattr = type.GetCustomAttributes(false).Where(attr => attr.GetType().Name == "TableAttribute").SingleOrDefault() as dynamic;
        //        if (tableattr != null)
        //        {
        //            name = tableattr.Name;
        //        }
        //        else
        //        {
        //            tableattr = type.GetCustomAttributes(true).Where(attr => attr.GetType().Name == "TableAttribute").SingleOrDefault() as dynamic;
        //            if (tableattr != null)
        //                name = tableattr.Name;
        //        }
        //        TypeTableNameMap[type.TypeHandle] = name;
        //    }
        //    return name;
        //}
    }

}
